const app = getApp()
const util = require('../../utils/util');
Page({
  data: {
    showPlv: true,// 播放插件是否显示
    vid: '',
    //beta: true,
    viewerInfo: { viewerId: 'polyv-test', viewerName: 'polyv测试',viewerAvatar: 'https://livestatic.videocc.net/assets/wimages/missing_face.png' },
    title: 'this is title',
    autoplay: true,
    poster: '',// 播放器封面图
    ban_history_time: false,// 是否禁用续播功能
    historyTimeType: 'remote',// 续播方式为远程
    pictureInPictureMode: ["push", "pop"],// 开启小窗
  },
  onLoad() {

    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

  },
  onReady() {
    this.init()
  },
  init() {// 初始播放器
    const vid = '88083abbf5bfc681ce7d8cdfb32e73f7_8';// 必填
    if(!vid) {
        wx.showModal({
          title: '提示',
          content: '请在/pages/index/index.js里面填写vid',
          success: function (res) {
              if (res.confirm) {
                  // console.log('用户点击确定')
              }
          }
      })
    }else {
      // 非加密视频
      this.setData({
        vid,
      })

      // 加密视频
      /**const vid1 = '';// 加密的vid
      const secretkey = ''; // 管理后台的secretkey，加密视频需要用到
      const sign = util.getSign(secretkey ,vid1);
      this.setData({
        vid: vid1,
        ts: sign.timestamp,
        sign: sign.sign,
      })**/
    }
  },
  onShow() {

  },
  onbinderror(e) {
    console.error('onbinderror', e)
  },
  onbindseekCompleted() {

  },
  getVideoPlayDuration() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    const s = polyvPlayerContext.getVideoPlayDuration();
    console.error(polyvPlayerContext);
    console.error(polyvPlayerContext.getVideoPlayDuration());
    wx.showModal({
      title:'提示',
      content: '当前视频已播放时长：'+ s
    })
  },
  changeVideo() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    polyvPlayerContext.changeVideo();
  },
  changeVod() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    polyvPlayerContext.pause()
    const currentTime = polyvPlayerContext.getCurrentTime();
    polyvPlayerContext.changeAudio();
    polyvPlayerContext.audioContext.audioContext.pause()
    setTimeout(() => {
      polyvPlayerContext.audioContext.audioContext.seek(currentTime)
    },1000)
  },
  getDuration() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    console.error('	获取视频时长',polyvPlayerContext.getDuration())
  },
  changeMuted() {// 开启静音播放
    this.setData({
      muted: true
    })
    try{
      const polyvPlayerContext = this.selectComponent('#polyvPlayer');
      polyvPlayerContext.audioContext.audioContext.volume = 0
    }catch(e) {

    }
  },
  changeMuted1() {// 关闭静音播放
    this.setData({
      muted: false
    })
    try{
      const polyvPlayerContext = this.selectComponent('#polyvPlayer');
      polyvPlayerContext.audioContext.audioContext.volume = 1
    }catch(e) {

    }
  },
  destroy() {
    this.setData({
      showPlv: false
    })
  },
  changeVid(e) {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    polyvPlayerContext.changeVid({
      vid: '88083abbf5bfc681ce7d8cdfb32e73f7_8',
    })
    // polyvPlayerContext.pause()
  },
  onended() {
    console.log('当播放到末尾时触发 ended 事件')
  },
  onbindloadedmetadata(e) {
    console.log('视频元数据加载完成时触发', e)
  },
  onbindprogress(e) {
    // console.log('加载进度变化时触发，只支持一段加载', e)
  },
  getQuality() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    console.log(polyvPlayerContext.getQuality())
    wx.showModal({
      title:'提示',
      content: JSON.stringify(polyvPlayerContext.getQuality())
    })
  },
  closeWeakNetworkTips() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    polyvPlayerContext.closeWeakNetworkTips();
  },
  showWeakNetworkTips() {
    const polyvPlayerContext = this.selectComponent('#polyvPlayer'); // 通过插件的自定义组件ID获取
    // 设置自定义的文案和关闭时的回调
    polyvPlayerContext.showWeakNetworkTips({
      customNormalText: '当前网络不佳1',
      customActiveText: ' 降低清晰度2',
      customSuccessText: '已经成功切换啦3！',
      position: 'right-top'
    })
  },
  onbindtimeupdate(e) {
    // console.log('播放进度变化时触发', e.detail)
  },
  onbindprogressDragMove(e) {
    console.log('触发于进度条圆点拖动过程中', e)
  },
  onbindcontrolstoggle(e) {
    console.log('切换 controls 显示隐藏时触发', e.detail)
  },
  onPlaying() {
    console.log('当开始/继续播放时触发playing事件')
  },
  onPause() {
    console.log('当暂停播放时触发 pause 事件')
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goWindow() {
    app.globalData.videoContext = this.selectComponent('#polyvPlayer');
    wx.navigateTo({
      url: '../logs/logs',
    })
  },
  onEnterpictureinpicture() {
    wx.showModal({
      title:'提示',
      content: '播放器进入小窗'
    })
    console.log('播放器进入小窗');
  },
  onLeavepictureinpicture() {
    wx.showModal({
      title:'提示',
      content: '播放器退出小窗'
    })
    console.log('播放器退出小窗');
  },
  onStateChange(e) {
    console.log('播放状态变更事件',e)
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
